package studyguide.ch06;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TickerSampleMidlet extends MIDlet {

	private final TextBox textBoxA;
	private final Ticker ticker;
	private final Command exitCommand;

	private class ExitCommandListener implements CommandListener {

		public void commandAction(Command c, Displayable d) {
			if (c == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else {
				throw new RuntimeException("Unknown command:" + c);
			}
		}
		
	}
	
	public TickerSampleMidlet() {
		System.out.println("Running constructor");
		textBoxA = new TextBox(
				"Ticker sample",
				"See a ticker up there!",
				50,
				TextField.UNEDITABLE);
		ticker = new Ticker("This is not very useful...");
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		textBoxA.setTicker(ticker);
		textBoxA.addCommand(exitCommand);
		textBoxA.setCommandListener(new ExitCommandListener());
	}
	
	protected void destroyApp(boolean unconditional) {
		System.out.println("Exiting application");
	}

	protected void pauseApp() {
		System.out.println("Pausing application");
	}

	protected void startApp() throws MIDletStateChangeException {
		System.out.println("Starting (or restarting) application");
		Display d = Display.getDisplay(this);
		// Always starts on first screen
		d.setCurrent(textBoxA);
	}

}
